package mo.leetcode_top;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class L49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> sameMap = new HashMap<>();
        char[] table = new char[26];
        for (String s : strs) {
            for (int i = 0; i < 26; i++) {
                table[i] = 0;
            }
            for (int i = 0; i < s.length(); i++) {
                table[s.charAt(i)-'a']++;
            }

            String s1 = String.valueOf(table);
            List<String> strings = sameMap.get(s1);
            if (strings == null) {
                strings = new ArrayList<>();
                sameMap.put(s1, strings);
            }
            strings.add(s);
        }
        return new ArrayList<>(sameMap.values());
    }

    public static void main(String[] args) {
        L49 test = new L49();
        System.out.println(test.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }
}
